package builder;

import mapping.Configuration;
import org.apache.commons.dbcp.BasicDataSource;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;
import java.util.Properties;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2020-01-08 10:42
 * @notify 解析mybatisConfig的类，当前解析 environments  mappers
 * 创建Configuration 设置DataSource
 *  mappers 则获取输入流，交给XMLMapperBuilder解析每一个mapper文件
 * @version 1.0
 */
public class XMLConfigBuilder {

    private Configuration configuration = new Configuration();

    public Configuration parse(Element rootElement) throws Exception {
        //解析<environments>
        Element environments = rootElement.element("environments");
        parseEnvironments(environments);
        //解析<mappers>
        Element mappers = rootElement.element("mappers");
        parseMappers(mappers);
        return configuration;
    }

    //解析<environments>
    private void parseEnvironments(Element environments) {
        //查询environments default="dev"
        String aDefault = environments.attributeValue("default");
        //获取全部的 environment
        List<Element> environment = environments.elements("environment");
        //循环所有的 environment
        for (Element env : environment) {
            //如果当前 environment 的id和默认的id相同则继续向下解析
            if (env.attributeValue("id").equals(aDefault)) {
                parseEnvironment(env);
            }
        }
    }

    //解析environment
    private void parseEnvironment(Element environment) {
        //解析<dataSource type="DBCP">
        Element dataSource = environment.element("dataSource");
        parseDataSource(dataSource);
    }

    //解析dataSource
    private void parseDataSource(Element dataSource) {
        //获取连接池类型
        String type = dataSource.attributeValue("type");
        //设置 <dataSource type="DBCP"> 连接池
        if (type.equals("DBCP")) {
            //创建 DBCP连接池
            BasicDataSource dataSource1 = new BasicDataSource();
            //创建配置类
            Properties properties = new Properties();
            //获取全部的property
            List<Element> propertys = dataSource.elements("property");
            //循环拿到<property name="driver" value="com.mysql.jdbc.Driver"/>
            for (Element prop : propertys) {
                //获取标签name属性值
                String name = prop.attributeValue("name");
                //获取标签value属性值
                String value = prop.attributeValue("value");
                //设置到配置类
                properties.put(name, value);
            }
            //设置连接池属性
            dataSource1.setDriverClassName(properties.get("driver").toString());
            dataSource1.setUrl(properties.get("url").toString());
            dataSource1.setUsername(properties.get("username").toString());
            dataSource1.setPassword(properties.get("password").toString());
            //给Configuration设置数据源信息
            configuration.setDataSource(dataSource1);
        }
    }

    //解析<mappers>
    private void parseMappers(Element mappers) throws Exception {
        //拿到所有的<mapper resource="mapper/UserMapper.xml"></mapper>
        List<Element> mapperElements = mappers.elements("mapper");
        //遍历解析每一个 mapper.xml
        for (Element mapperElement : mapperElements) {
            parseMapper(mapperElement);
        }
    }

    //解析每一个mapper标签
    private void parseMapper(Element mapperElement) throws Exception {
        //TODO 此处还有url等方式
        String resource = mapperElement.attributeValue("resource");
        //根据文件名获取输入流
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resource);
        //dom4j解析
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(inputStream);
        //获取跟标签
        Element rootElement = document.getRootElement();
        XMLMapperBuilder mapperBuilder = new XMLMapperBuilder(configuration);
        mapperBuilder.parse(rootElement);
    }


}
